package com.magic.gre.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.magic.gre.R;
import com.magic.gre.adapter.WordsAdapter;
import com.magic.gre.base.activity.BaseMVPActivity;
import com.magic.gre.entity.WordsBean;
import com.magic.gre.helper.Contract;
import com.magic.gre.mvp.contract.NewWordsContract;
import com.magic.gre.mvp.presenter.NewWordsPresenterImpl;
import com.noname.lib_base_java.entity.MsgEvent;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: Lmoumou
 * Date: 2019-01-07 17:45
 * 文件名: NewWordsActivity
 * 描述:
 */
public class NewWordsActivity extends BaseMVPActivity<NewWordsPresenterImpl> implements NewWordsContract.View,
        WordsAdapter.OnWordsListener {

    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.start_study_tv)
    TextView startStudyTv;

    public static void startThis(Context context) {
        Intent intent = new Intent(context.getApplicationContext(), NewWordsActivity.class);
        context.startActivity(intent);
    }

    private WordsAdapter adapter;
    private List<WordsBean> dataList = new ArrayList<>();
    private int pageNo = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_new_words;
    }

    @Override
    protected NewWordsPresenterImpl creatPresenter() {
        return new NewWordsPresenterImpl(this);
    }

    @Override
    protected void netOverride() {
        pageNo = 1;
        mPresenter.pNewWordsList(pageNo);
    }

    @Override
    protected void handlerMsg(MsgEvent msgEvent) {
        super.handlerMsg(msgEvent);
        switch (msgEvent.getEvent()){
            case MsgEvent.NEW_WORDS_CHANGE:
                netOverride();
                break;
        }
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        registerRxBus();
        adapter = new WordsAdapter(this, dataList, WordsAdapter.NEW_WORDS);
        adapter.setOnWordsListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mRecyclerView.setAdapter(adapter);
        showLoadDialog();
        netOverride();
    }

    @OnClick(value = {R.id.start_study_tv})
    protected void onClick(View view){
        switch (view.getId()){
            case R.id.start_study_tv:
                WordsDetailsActivity.startThis(this,Contract.NEW_WORDS_ID,0);
                break;
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        super.onRefresh(refreshLayout);
        netOverride();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        super.onLoadMore(refreshLayout);
        mPresenter.pNewWordsList(pageNo);
    }

    @Override
    public void vNewWordsList(List<WordsBean> wordsBeanList) {
        if (pageNo == 1) {
            dataList.clear();
            if (wordsBeanList == null || wordsBeanList.isEmpty()) {
                showEmpty();
                startStudyTv.setVisibility(View.GONE);
                stopRefresh(true, false);
                adapter.notifyDataSetChanged();
                return;
            }
        }
        pageNo++;
        dataList.addAll(wordsBeanList);
        adapter.notifyDataSetChanged();
        showContent();
        stopRefresh(true, wordsBeanList.size() == Contract.PAGER_SIZE);
    }

    @Override
    public void onSelectAll() {

    }

    @Override
    public void onNotSelectAll() {

    }

    @Override
    public void onItemClickListener(int position) {
        WordsDetailsActivity.startThis(this,Contract.NEW_WORDS_ID,position);
    }
}
